package com.cnpinxi.controller.admin.resource;

import com.cnpinxi.controller.base.BaseController;
import com.cnpinxi.def.Constant;
import com.cnpinxi.domain.Resource;
import com.cnpinxi.domain.ResourceRelation;
import com.cnpinxi.result.ResultObj;
import com.cnpinxi.service.ResourceRelationService;
import com.cnpinxi.service.ResourceService;
import com.cnpinxi.util.StringUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * Created by Baggio on 2017-11-15.
 */
@RequestMapping("/admin/resource")
@Controller
public class AdminResourceController extends BaseController {
    @Autowired
    ResourceService resourceService;    @Autowired
    ResourceRelationService resourceRelationServiceService;
    /**
     * 获取资源列表
     * @param request
     * @return
     */
    @RequestMapping("/getResourceList")
    public ModelAndView getProductList(HttpServletRequest request, Integer page, Integer pageSize) {
        ModelAndView m = new ModelAndView("/admin/resource/getResourceList");
        Map<String, String> map = this.showRequestParameters(request);
        if (page == null || page == 0) {
            page = Constant.PAGENUM;
        }
        // 设置每页记录数
        if (pageSize == null || pageSize == 0) {
            pageSize = Constant.PAGESIZENum;
        }
        map.put("page",String.valueOf(page));
        map.put("pageSize",String.valueOf(pageSize));
        PageHelper.startPage(page, pageSize);
        List<Resource> resourceList = resourceService.getResourceList(map);
        PageInfo<Resource> pageInfo = new PageInfo<>(resourceList);
        m.addObject("pageInfo", pageInfo);
        m.addObject("bean",map);
        return m;
    }

    /**
     * 编辑资源提交
     * @param request
     * @return
     */
    @RequestMapping(value = "/resource")
    public ModelAndView resource(HttpServletRequest request){
        ModelAndView m = new ModelAndView("/admin/resource/resource");
        Map<String, String> map = this.showRequestParameters(request);
        String id=map.get("tid");
        if(StringUtil.isNotEmpty(id)){
            Resource resource=   resourceService.getResource(map);
            m.addObject("resource",resource);
        }

        m.addObject("bean",map);

        return m;
    }
    @RequestMapping(value = "/getResourceRelationList")
    public ModelAndView getResourceRelationList(HttpServletRequest request){
        ModelAndView m = new ModelAndView("/admin/resource/getResourceRelationList");
        Map<String, String> map = this.showRequestParameters(request);
        List<ResourceRelation> resourceRelation=   resourceRelationServiceService.getResourceRelationList(map);
        PageInfo<ResourceRelation> pageInfo = new PageInfo<>(resourceRelation);
        m.addObject("pageInfo", pageInfo);
        m.addObject("bean",map);

        return m;
    }

    /**
     * 编辑资源提交
     * @param resource
     * @return
     */
    @RequestMapping(value = "/deleteResourceRelation")
    @ResponseBody
    public String deleteResourceRelation(Long tid){
        ResultObj resultObj = new ResultObj();
        resultObj.setCode(ResultObj.CODE_SUCCESS);
        String resourceUrl="";
        try {



            resourceRelationServiceService.deleteResourceRelation(tid);
            resultObj.setMsg("操作成功！");
        } catch (Exception e) {
            resultObj.setCode(ResultObj.CODE_FAIL);
            resultObj.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return resultObj.toJson();
    }


    @RequestMapping(value = "/resourceRelation")
    public ModelAndView resourceRelation(HttpServletRequest request){
        ModelAndView m = new ModelAndView("/admin/resource/resourceRelation");
        Map<String, String> map = this.showRequestParameters(request);
        String tid=map.get("tid");
        if(StringUtil.isNotEmpty(tid)){
            ResourceRelation resourceRelation=  resourceRelationServiceService.getResourceRelation(map);
            m.addObject("resourceRelation",resourceRelation);
        }

        m.addObject("bean",map);

        return m;
    }
    /**
     * 编辑资源提交
     * @param resource
     * @return
     */
    @RequestMapping(value = "/modifyResource")
    @ResponseBody
    public String modifyResource(HttpServletRequest request,Resource resource){
        ResultObj resultObj = new ResultObj();
        resultObj.setCode(ResultObj.CODE_SUCCESS);
        String resourceUrl="";
        try {

            try {
                Map<String,String> fileMap= upLoadFile(request);
                resourceUrl=fileMap.get("location");
                logger.info("resourceUrl=============>>>"+resourceUrl);
                resource.setResourceUrl(resourceUrl);
            } catch (Exception e) {
                e.printStackTrace();
            }

            resourceService.modifyResource(resource);
            resultObj.setMsg("操作成功！");
        } catch (Exception e) {
            resultObj.setCode(ResultObj.CODE_FAIL);
            resultObj.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return resultObj.toJson();
    }  /**
     * 编辑资源提交
     * @param resourceRelation
     * @return
     */
    @RequestMapping(value = "/modifyResourceRelation")
    @ResponseBody
    public String modifyResourceRelation(HttpServletRequest request,ResourceRelation resourceRelation){
        ResultObj resultObj = new ResultObj();
        resultObj.setCode(ResultObj.CODE_SUCCESS);
        String resourceUrl="";
        try {

            resourceRelationServiceService.modifyResourceRelation(resourceRelation);
            resultObj.setMsg("操作成功！");
        } catch (Exception e) {
            resultObj.setCode(ResultObj.CODE_FAIL);
            resultObj.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return resultObj.toJson();
    }  /**
     * 编辑资源提交
     * @param resource
     * @return
     */
    @RequestMapping(value = "/resourceList")
    @ResponseBody
    public String resourceList(HttpServletRequest request, Integer page, Integer pageSize){
        ResultObj resultObj = new ResultObj();
        resultObj.setCode(ResultObj.CODE_SUCCESS);
        try {

            Map<String, String> map = this.showRequestParameters(request);
            if (page == null || page == 0) {
                page = Constant.PAGENUM;
            }
            // 设置每页记录数
            if (pageSize == null || pageSize == 0) {
                pageSize = Constant.PAGESIZENum;
            }
            map.put("page",String.valueOf(page));
            map.put("pageSize",String.valueOf(pageSize));
            List<Resource> resourceList = resourceService.getResourceList(map);
            PageInfo<Resource> pageInfo = new PageInfo<>(resourceList);
            resultObj.setData(pageInfo);
            resultObj.setMsg("操作成功！");
        } catch (Exception e) {
            resultObj.setCode(ResultObj.CODE_FAIL);
            resultObj.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return resultObj.toJson();
    }

}
